package interfaze;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JList;
import javax.swing.ListModel;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Rectangle;

import javax.swing.SwingConstants;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.border.LineBorder;

public class ListWindow extends JFrame implements ActionListener, MouseListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	JComboBox comboBox;
	JList list;
	JButton btnMenu;
	menuPopUp mP;
	Thread checker = new checkerThread();
	PlayWindow player = new PlayWindow();
	boolean performCheck = true;
	Color background = Color.black;

	public ListWindow() {
		getContentPane().setForeground(background);
		getContentPane().setLayout(new BorderLayout(0, 0));

		JPanel panel_4 = new JPanel();
		panel_4.setBackground(background);
		getContentPane().add(panel_4, BorderLayout.NORTH);
		panel_4.setLayout(new BoxLayout(panel_4, BoxLayout.Y_AXIS));

		JPanel panel = new JPanel();
		panel.setBackground(background);
		panel_4.add(panel);
		panel.setLayout(new BorderLayout(0, 0));

		JPanel panel_3 = new JPanel();
		panel_3.setBackground(background);
		panel.add(panel_3, BorderLayout.WEST);
		panel_3.setLayout(new BoxLayout(panel_3, BoxLayout.X_AXIS));

		panel_3.setLayout(new BorderLayout());
		btnMenu = new JButton("Menu");
		panel_3.add(btnMenu, BorderLayout.WEST);
		// btnMenu.setHorizontalAlignment(SwingConstants.LEFT);

		JLabel lblList = new JLabel();
		lblList.setIcon(new ImageIcon("list.png"));
		panel.add(lblList, BorderLayout.EAST);
		// lblList.setHorizontalAlignment(SwingConstants.RIGHT);

		JPanel panel_1 = new JPanel();
		panel_1.setBackground(background);
		panel_4.add(panel_1);
		panel_1.setLayout(new BoxLayout(panel_1, BoxLayout.X_AXIS));

		comboBox = new JComboBox();
		comboBox.setModel(new DefaultComboBoxModel(new String[] { "Album",
				"Artista", "Genero", "Listas de reproduccion" }));
		comboBox.setSelectedIndex(0);
		panel_1.add(comboBox);

		JPanel panel_2 = new JPanel();
		panel_2.setBorder(new LineBorder(Color.DARK_GRAY, 2, true));
		panel_2.setBackground(background);
		getContentPane().add(panel_2);

		list = new JList();
		panel_2.add(list);

		btnMenu.addActionListener(this);
		comboBox.addActionListener(this);

		this.setSize(300, 500);
		this.setMinimumSize(new Dimension(300, 500));
		this.setResizable(false);
		checker.start();
	}

	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if (e.getSource().equals(btnMenu)) {
			if (mP == null) {
				mP = new menuPopUp(this);
				mP.setLocation(btnMenu.getX() + this.getX() + 20,
						btnMenu.getY() + this.getY() + 40);
				mP.setVisible(true);
				// System.out.println(btnMenu.getX()+this.getX()
				// +", "+btnMenu.getY()+this.getY());
			} else {
				mP.setVisible(false);
				mP = null;
			}

		} else if (e.getSource().equals(comboBox)) {
			switch (comboBox.getSelectedIndex()) {
			case 0:
				ArrayList<String> a = data_base.searches.albumes();

				break;
			case 1:
				// list.setModel(getListaArtista());
				break;
			case 2:
				// list.setModel(getListaGenero());
				break;
			case 3:
				// list.setModel(getListaListas());
				break;
			default:
			}
		}
	}

	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	class checkerThread extends Thread {
		public void run() {
			check();
		}
	}

	public void check() {
		while (performCheck) {
			try {
				checker.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			if (!this.isVisible() && player == null) {

				mP = null;
				this.performCheck = false;
				this.dispose();
				System.exit(0);
			} else if (player != null) {
				if (!player.isVisible() && !this.isVisible()) {

					mP = null;
					player.performCheck = false;
					player.dispose();
					this.performCheck = false;
					this.dispose();
					System.exit(0);
				}
			} else {

			}
		}
	}
}
